package com.msb.serviceDriverUser.service;

import com.msb.internalcommon.dto.Car;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.TerminalResponse;
import com.msb.internalcommon.reponese.TrackResponse;
import com.msb.serviceDriverUser.mapper.CarMapper;
import com.msb.serviceDriverUser.remote.ServiceMapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.H;

/**
 * @Auther: jiudianliu
 * @Date: 2023/7/1 - 07 - 01 - 19:26
 * @Description: com.msb.serviceDriverUser.service
 * @version: 1.0
 */
@Service
public class CarService {

    @Autowired
    private CarMapper carMapper ;

    @Autowired
    private ServiceMapClient serviceMapClient ;


    /**
     * 将车辆信息插入到数据库
     * @param car
     * @return
     */
    public ResponseResult addCar(Car car){
        LocalDateTime now = LocalDateTime.now();
        car.setGmtModified(now);
        car.setGmtCreate(now);

        //保存车辆
        carMapper.insert(car);

        // 根据数据库中的车牌号获得对应车辆tid
        ResponseResult<TerminalResponse> responseResult = serviceMapClient.addTerminal(car.getVehicleNo() , car.getId() +"");
        String tid = responseResult.getData().getTid();
        car.setTid(tid);

        //根据tid获得该车辆的轨迹编号：trid  存入数据库
        ResponseResult<TrackResponse> trackResponseResponseResult = serviceMapClient.addTrack(tid);
        String trid = trackResponseResponseResult.getData().getTrid();
        String trname = trackResponseResponseResult.getData().getTrname();
        car.setTrid(trid); car.setTrname(trname);

        carMapper.updateById(car);
        return ResponseResult.success("");
    }

    /**
     * 根据车辆的id 查询出数据
     * @param carId
     * @return
     */
    public ResponseResult<Car> getCarById(Long carId){
        Map<String , Object> map = new HashMap<>() ;
        map.put("id" , carId) ;
        List<Car> cars = carMapper.selectByMap(map);
        return ResponseResult.success(cars.get(0));
    }
}
